package com.zhenyi.xiaohuoche.admin;

import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xhc.common.model.XhcUserPresent;
import com.zhenyi.xhc.core.XHCAuthInterceptor;
import com.zhenyi.xhc.core.XHCController;

@Before(XHCAuthInterceptor.class)
public class AdminOrderController extends XHCController {

	/**
	 * 票务订单(userTicket)
	 * 按照年与日至年月日筛选,默认显示当日,微信昵称,购票时段,下单时间,支付金额,支付状态(status:1已支付,-1退款,0未支付)
	 */
	public void tickets() {
		Date start = getStartDate();
		Date end = getEndDate();
		SqlPara sqlPara = Db.getSqlPara("AdminUserTickets", start, end);
		Page<Record> pages = Db.paginate(getPage(), PAGE_SIZE, sqlPara);
		renderPage(pages);
	}

	/**
	 * 小礼品订单(userPresent) 按照年与日至年月日筛选,默认显示当日 微信昵称,标题,购买数量,支付金额,状态,快递单号
	 */
	public void presents() {
		Date start = getStartDate();
		Date end = getEndDate();
		SqlPara sqlPara = Db.getSqlPara("AdminPresentsOrder", start, end);
		Page<Record> pages = Db.paginate(getPage(), PAGE_SIZE, sqlPara);
		renderPage(pages);
	}

	/**
	 * 订单详情,标题,支付金额,状态,物流信息
	 */
	public void presentInfo() {
		Long id = getParaToLong("id");
		XhcUserPresent userPresent = XhcUserPresent.dao.findById(id);
		renderResult(userPresent);
	}

	/**
	 * 更新物流信息,快递单号
	 */
	@Before(Tx.class)
	public void updatePresent() {
		Long id = getParaToLong("id");
		String telNumber = getPara("telNumber");
		String userName = getPara("userName");
		String address = getPara("address");
		XhcUserPresent userPresent = XhcUserPresent.dao.findById(id);
		String trackNo = getPara("trackNo");
		userPresent.setTrackNo(trackNo);
		if (StrKit.notBlank(trackNo)) {
			userPresent.setStatus(2);
		}
		userPresent.setAddress(address);
		userPresent.setUserName(userName);
		userPresent.setTelNumber(telNumber);
		renderFlag(userPresent.update());
	}

	/**
	 * 所有订单(orders) 按照年与日至年月日筛选,默认显示当日
	 */
	public void orders() {
		Date start = getStartDate();
		Date end = getEndDate();
		SqlPara sqlPara = Db.getSqlPara("AdminOrders", start, end);
		Page<Record> pages = Db.paginate(getPage(), PAGE_SIZE, sqlPara);
		renderPage(pages);
	}

}
